package com.nexen.quartz.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.nexen.common.core.domain.entity.SysRole;
import com.nexen.common.core.domain.entity.SysUser;
import com.nexen.common.utils.StringUtils;
import com.nexen.common.utils.aliyun.AliyunSendSMSUtil;
import com.nexen.framework.manager.AsyncManager;
import com.nexen.framework.manager.factory.AsyncFactory;
import com.nexen.merchant.serviceRequest.domain.NexenServicerequest;
import com.nexen.merchant.serviceRequest.service.INexenServicerequestService;
import com.nexen.system.domain.SysOperLog;
import com.nexen.system.service.ISysRoleService;
import com.nexen.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.aliyun.teautil.Common.assertAsString;

/**
 * <p>Title: NexenServicerequestTask</p>
 * <p>Description: 换胎申请，通知运营人员短信，定时任务调度测试</p>
 *
 * @author cong.zhang
 * @version 1.0.0
 * @date 2024/8/16
 * @time 23:37
 */
@Component("serviceRquestTask")
public class NexenServicerequestTask {

    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private INexenServicerequestService servicerequestService;

    public void sendSMS() {
        // 获取运营角色id
        SysRole sysRole = sysRoleService.selectRoleByRoleKey("SMS_REC");
        if (ObjUtil.isNotNull(sysRole)){
            // 1. 获取系统运营发送短信相关的手机号
            Long roleId = sysRole.getRoleId();
            // 查询相关角色下的所有用户
            List<String> sendPhoneList = new ArrayList<>();
            List<SysUser> sysUserList = sysUserService.selectRoleGroupAllUser(roleId);
            for (SysUser user : sysUserList) {
                sendPhoneList.add(user.getPhonenumber());
            }
            
            // 2. 查询所有未发送短信的换胎申请
            NexenServicerequest nexenServicerequest = new NexenServicerequest();
            nexenServicerequest.setSendSms(1);  // 查询所有未发送短信的换胎申请服务
            List<NexenServicerequest> nexenServicerequestList = servicerequestService.selectAllList(nexenServicerequest);
            
            // 3. 调用短信发送接口进行短信发送
            if (ArrayUtil.isNotEmpty(sendPhoneList) &&
                    sendPhoneList.size() > 0 &&
                    ArrayUtil.isNotEmpty(nexenServicerequestList) &&
                    nexenServicerequestList.size() > 0) {
                String content = "";
                List<Long> ids = new ArrayList<>();
                Set<String> cityList = new HashSet<>();     // 去除相同身份城市地址
                for (NexenServicerequest servicerequest : nexenServicerequestList) {
                    ids.add(servicerequest.getId());
                    //content += servicerequest.getProvince() + "-" + servicerequest.getName() + " - " + servicerequest.getMobile() + "\n";

                    // 地址记录
                    cityList.add(servicerequest.getProvince() + "-" + servicerequest.getCity());
                }
                // 根据获取的换胎服务，发送短信给后台审核人员
                for (String address : cityList) {
                    // 发送换胎服务短信给后台管理审核人员
                    for (String phone : sendPhoneList) {
                        JSONObject jsonObject = new JSONObject().set("address", address);
                        SendSmsResponse sendSmsResponse = AliyunSendSMSUtil.sendAuditSMS(phone, jsonObject.toString(), "耐克森轮胎", "SMS_472395269");
//                        System.out.println(sendSmsResponse.getBody().toString());
                    }
                }
                //SendSmsResponse sendSmsResponse = AliyunSendSMSUtil.sendAuditSMS("18321822734", "耐克森轮胎", "SMS_472130002");

                // 更新换胎申请，发送短信状态
                int i = 0;
                // 更新换胎申请，发送短信状态
                if (ArrayUtil.isNotEmpty(ids) && ids.size()>0) {
                    i = servicerequestService.updateNexenServicerequestSendSMSByIds(ids);
                }

                // 异步记录日志
                SysOperLog sysOperLog = new SysOperLog();
                sysOperLog.setTitle("换胎申请-定时任务发送审核短信");
                sysOperLog.setBusinessType(2);
                sysOperLog.setOperatorType(1);
                sysOperLog.setRequestMethod("POST");
                sysOperLog.setOperParam(sendPhoneList.toString());
                sysOperLog.setOperName("admin");
                sysOperLog.setJsonResult(i>0 ? "短信发送状态更新成功" : "短信发送状态更新失败");
                sysOperLog.setOperIp("内网IP");
                sysOperLog.setStatus(0);
                sysOperLog.setOperTime(DateUtil.date());
                AsyncManager.me().execute(AsyncFactory.recordOper(sysOperLog));
                //System.out.println("发送短信数据成功，接收人信息：" + sendPhoneList + "，请及时处理换胎服务申请的审核：" + content);
            }
        }

        System.out.println("执行换胎申请，短信发送定时任务");
    }

}
